package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.GoodsService;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 商品信息Controller
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("/goods/listInventory")
    public Map<String,Object> GoodsInventory(Integer page, Integer rows,String codeOrName, Integer goodsTypeId){
        Map<String, Object> stringObjectMap = goodsService.GoodsInventory(page, rows, codeOrName, goodsTypeId);
        return stringObjectMap;

    }

//    /**
//     * 查询商品所有分类
//     * @return 商品类别JSON
//     */
//    @PostMapping("/goodsType/loadGoodsType")
//    public Map<String,Object> loadGoodsType(){
//
//        return goodsService.loadGoodsType();
//    }

    /**
     * 根据父节点获取所有商品类别
     * @return
     * @throws Exception
     */
    @PostMapping("/goodsType/loadGoodsType")
    public String loadTreeInfo() {
        return getAllByParentId(-1).toString();
    }

    /**
     * 根据父节点id获取商品所有类别集合
     * @param parentId
     * @return
     */
    public JsonArray getAllByParentId(Integer parentId) {
        JsonArray jsonArray=this.getByParentId(parentId);
        for(int i=0;i<jsonArray.size();i++) {
            JsonObject jsonObject=(JsonObject) jsonArray.get(i);
            if("open".equals(jsonObject.get("state").getAsString())) {
                continue;
            }else {
                jsonObject.add("children", getAllByParentId(jsonObject.get("id").getAsInt()));
            }

        }
        return jsonArray;
    }

    /**  获取同一层的子节点
     * 根据父节点查询所有子节点
     * @param parentId
     * @return
     */
    public JsonArray getByParentId(Integer parentId) {

        List<GoodsType> goodsTypeList=goodsService.findByParentId(parentId);
        JsonArray jsonArray=new JsonArray();
        for(GoodsType goodsType:goodsTypeList) {
            JsonObject jsonObject=new JsonObject();
            jsonObject.addProperty("id", goodsType.getGoodsTypeId()); // 节点Id
            jsonObject.addProperty("text", goodsType.getGoodsTypeName()); // 节点名称
            if(goodsType.getGoodsTypeState()==1) {  // state属性  当删除菜单时 要根据state判断 根节点不能删除
                jsonObject.addProperty("state", "closed");// 根节点
            }else {
                jsonObject.addProperty("state", "open"); // 叶子节点
            }


            jsonObject.addProperty("iconCls", "good-type");// 节点图标

            //存放扩展属性
            JsonObject attributeObject=new JsonObject(); // 扩展属性
            attributeObject.addProperty("state", goodsType.getGoodsTypeState());

            jsonObject.add("attributes", attributeObject);

            jsonArray.add(jsonObject);
        }

        return jsonArray;
    }

    /**
     * 查询所有商品单位
     * @return
     */
    @PostMapping("/unit/list")
    public Map<String,Object> list(){
        Map<String, Object> stringObjectMap = goodsService.listUnit();
        return stringObjectMap;
    }


    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("/goods/list")
    public Map<String,Object> selectAllGoodsMsg(Integer page, Integer rows, String goodsName, Integer goodsTypeId){
        Map<String, Object> stringObjectMap = goodsService.selectAllGoodsMsg(page-1, rows, goodsName, goodsTypeId);
        return stringObjectMap;
    }



    /**
     * 生成商品编码
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }

    /**
     * 商品添加或修改
     * @param goods 商品信息实体
     * @return
     */
    @PostMapping("/goods/save")
    public ServiceVO saveGoodsMsg(Goods goods){
        ServiceVO serviceVO = goodsService.saveGoodsMsg(goods);
        return serviceVO;
    }

    /**
     * 删除商品信息
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("/goods/delete")
    public ServiceVO deleteGoodsMsg(Integer goodsId){
        ServiceVO serviceVO = goodsService.deleteGoodsMsg(goodsId);
        return serviceVO;
    }

    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("/goods/getNoInventoryQuantity")
    public Map<String,Object> selectNullRepertory(Integer page,Integer rows,String nameOrCode){
        Map<String, Object> stringObjectMap = goodsService.selectNullRepertory(page-1, rows, nameOrCode);
        return stringObjectMap;
    }



    /**
     * 分页查询有库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("/goods/getHasInventoryQuantity")
    public Map<String,Object> selectRepertory(Integer page,Integer rows,String nameOrCode){
        Map<String, Object> stringObjectMap = goodsService.selectRepertory(page-1, rows, nameOrCode);
        return stringObjectMap;
    }


    /**
     * 添加商品期初库存
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */
    @PostMapping("/goods/saveStock")
    public ServiceVO addGoodsRepertory(Integer goodsId,Integer inventoryQuantity,double purchasingPrice){
        ServiceVO serviceVO = goodsService.addGoodsRepertory(goodsId,inventoryQuantity,purchasingPrice);
        return serviceVO;
    }

    /**
     * 删除商品库存
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("/goods/deleteStock")
    public ServiceVO deleteGoodsRepertory(Integer goodsId){
        ServiceVO serviceVO = goodsService.deleteGoodsRepertory(goodsId);
        return serviceVO;
    }
    /**
     * 查询库存报警商品信息
     * @return
     */
    @PostMapping("/goods/listAlarm")
    public Map<String,Object> listAlarm(){
        Map<String, Object> stringObjectMap = goodsService.selectListAlarm();
        return stringObjectMap;
    }


    /**
     * 分页查询供应商
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @PostMapping("supplier/list")
    public Map<String,Object> supplierList(Integer page, Integer rows,String supplierName){
        Map<String, Object> stringObjectMap = goodsService.supplierList(page, rows, supplierName);
        return stringObjectMap;
    }


    /**
     * 添加或修改供应商
     * @return
     */
    @RequestMapping("/supplier/save")
    public ServiceVO supplierSaveAndEdit(Supplier supplier){
        return goodsService.supplierSaveAndUpdate(supplier);
    }


    /**
     * 删除供应商
     * @param ids
     * @return
     */
    @RequestMapping("/supplier/delete")
    public ServiceVO supplierDelete( String  ids){
        return goodsService.supplierDelete(ids);
    }


    /**
     * 分页查询客户信息
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @PostMapping("customer/list")
    public Map<String,Object> customerList(Integer page, Integer rows, String  customerName){
        Map<String, Object> stringObjectMap = goodsService.customerList(page-1, rows, customerName);
        return stringObjectMap;
    }

    /**
     * 添加或修改客户信息
     * @return
     */
    @RequestMapping("customer/save")
    public ServiceVO customerSaveAndEdit(Customer customer){
        return goodsService.customerSaveAndUpdate(customer);
    }

    /**
     * 删除客户信息
     * @param ids
     * @return
     */
    @RequestMapping("customer/delete")
    public ServiceVO customerDelete(String  ids){
        return goodsService.customerDelete(ids);
    }


    /**
     * 新增分类
     * @param
     * @return
     */
    @PostMapping("/goodsType/save")
    public ServiceVO saveGoodsType(String  goodsTypeName,Integer  pId){
        return goodsService.saveGoodsType(goodsTypeName,pId);

    }


    /**
     * 删除分类
     * @param goodsTypeId
     * @return
     */
    @PostMapping("/goodsType/delete")
    public ServiceVO deleteGoodsType(Integer goodsTypeId){
        return goodsService.deleteGoodsType(goodsTypeId);
    }


    /**
     * 新增报损单
     * @param damageList
     * @param damageListGoodsStr
     * @return
     */
    @PostMapping("/damageListGoods/save")
    public ServiceVO saveDamageList(DamageList damageList, String damageListGoodsStr){
        return goodsService.saveDamageList(damageList,damageListGoodsStr);
    }

    /**
     * 新增报溢单
     * @param overflowList
     * @param overflowListGoodsStr
     * @return
     */
    @PostMapping("/overflowListGoods/save")
    public ServiceVO saveOverflowListGoods(OverflowList overflowList, String overflowListGoodsStr){
        return goodsService.saveOverflowListGoods(overflowList,overflowListGoodsStr);
    }

    /**
     * 报损单查询
     */
    @PostMapping("/damageListGoods/list")
    public Map<String,Object> selectDamageListGoods(String  sTime, String  eTime){


        Map<String, Object> stringObjectMap = goodsService.selectDamageListGoods(sTime,eTime);
        return stringObjectMap;
    }

    /**
     * 报溢单查询
     */
    @PostMapping("/overflowListGoods/list")
    public Map<String,Object> selectOverflowListGoods(String  sTime, String  eTime){


        Map<String, Object> stringObjectMap = goodsService.selectOverflowListGoods(sTime,eTime);
        return stringObjectMap;
    }


    @PostMapping("/damageListGoods/goodsList")
    public Map<String,Object> selectDamageListGoodsList(Integer damageListId){
        Map<String, Object> stringObjectMap = goodsService.selectDamageListGoodsList(damageListId);
        return stringObjectMap;
    }

    @PostMapping("/overflowListGoods/goodsList")
    public Map<String,Object> selectOverflowListGoodsList( Integer overflowListId){
        Map<String, Object> stringObjectMap = goodsService.selectOverflowListGoodsList(overflowListId);
        return stringObjectMap;
    }




}
